<template>
  <!-- 添加或修改项目基础信息管理对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body width="80%">
    <el-form :model="form" :rules="rules" label-width="125px" ref="form">
      <el-tabs  type="card" v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="框架编号" prop="projectCode">
                <el-input placeholder="请输入框架编号" v-model="form.projectCode" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="订单编号" prop="code">
                <el-input placeholder="请输入订单编号" v-model="form.code" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="业务员" prop="salesmanId">
                <el-select v-model="form.salesmanId" placeholder="请选择业务员" filterable clearable>
                  <el-option v-for="item in salesmanList" :key="item.userId" :label="item.nickName"
                    :value="item.userId" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="买方/开票单位" prop="customId">
                <el-select @change="selectCustomerForPrj" clearable filterable placeholder="请选择买方" style="width: 100%;"
                  v-model="form.customId">
                  <el-option :key="dict.value" :label="dict.label + '(' + dict.code + ')'" :value="dict.value"
                    ref="myselectCustomerForPrj" v-for="dict in dict.type.goods_customer" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="开票单位编码" prop="taxcode">
                <el-input placeholder="请输入开票单位编码" v-model="form.taxcode" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="项目单位" prop="companyName">
                <el-input v-model="form.companyName" placeholder="请输入项目单位" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="合同签订日期" prop="contractDate">
                <el-date-picker clearable placeholder="请选择合同签订日期" type="date" v-model="form.contractDate"
                  value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="订单金额" prop="contractMoney">
                <el-input-number :precision="2" @change="updateAuditMoney" placeholder="请输入订单金额"
                  v-model="form.contractMoney" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="已回款金额" prop="backMoney">
                <el-input-number :precision="2" disabled placeholder="请输入已回款金额" v-model="form.backMoney" />
              </el-form-item>
            </el-col>
            <!-- <el-col :span="8">
              <el-form-item label="剩余回款" prop="remainMoney">
                <el-input-number :precision="2" placeholder="请输入剩余回款" v-model="form.remainMoney"/>
              </el-form-item>
            </el-col>-->
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="剩余回款" prop="remainMoney">
                <el-input-number :precision="2" disabled placeholder="请输入剩余回款" v-model="form.remainMoney" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="已开票金额" prop="taxAll">
                <el-input-number :precision="2" disabled placeholder="请输入剩余回款" v-model="form.taxAll" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="剩余开票金额" prop="taxRemain">
                <el-input-number :precision="2" disabled placeholder="请输入剩余开票金额" v-model="form.taxRemain" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="费用金额" prop="feeMoney">
                <el-input-number :precision="2" disabled placeholder="请输入费用金额" v-model="form.feeMoney" />
              </el-form-item>
            </el-col>
            <!--<el-col :span="8">
              <el-form-item label="销售金额" prop="saleMoney">
                <el-input-number :precision="2" disabled placeholder="请输入费用金额" v-model="form.saleMoney" />
              </el-form-item>
            </el-col>-->
            <el-col :span="8">
              <el-form-item label="销售成本" prop="buyMoney">
                <el-input-number :precision="2" disabled placeholder="请输入销售成本" v-model="form.buyMoney" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="利润" prop="profitMoney">
                <el-input-number :precision="2" disabled placeholder="请输入费用金额" v-model="form.profitMoney" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="所属年度" prop="year">
                <el-date-picker clearable placeholder="请选择所属年度" type="year" v-model="form.year" value-format="yyyy">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="8">
              <el-form-item label="所属部门" prop="deptId">
                <treeselect :options="deptOptions" :show-count="true" @select="selectDept" placeholder="请选择单位"
                  v-model="form.deptId" />
              </el-form-item>
            </el-col>-->
            <el-col :span="8">
              <el-form-item label="回款状态" prop="fundCallback">
                <el-select clearable placeholder="回款状态" v-model="form.fundCallback">
                  <el-option :key="dict.value" :label="dict.label" :value="dict.value"
                    v-for="dict in dict.type.invoice_back_status" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- <el-row :gutter="24">
            <el-col :span="8">
             <el-form-item label="销售类型" prop="xstype">
               <el-input v-model="form.xstype" placeholder="请输入销售类型" />
             </el-form-item>
           </el-col>
           <el-col :span="8">
             <el-form-item label="业务类型" prop="ywtype">
               <el-select v-model="form.ywtype" placeholder="请选择业务类型" clearable>
                 <el-option v-for="dict in dict.type.yewu_type" :key="dict.value" :label="dict.label"
                   :value="dict.value" />
               </el-select>
             </el-form-item>
           </el-col>
         </el-row>-->
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="备注信息" prop="information">
                <el-input placeholder="请输入内容" type="textarea" v-model="form.information" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-tabs class="detail-tab" type="card" v-model="activeName">
            <el-tab-pane label="订单产品" name="first">
              <goods-sale-list-for-prj-form :projectId="form.id" :type="type2"
                ref="GoodsSaleListForPrjForm"></goods-sale-list-for-prj-form>
              <!--        <project-order-info-list-for-prj-form ref="ProjectOrderInfoListForPrjForm" :projectId="form.id" :projectName="form.name" :companyName="form.companyName"></project-order-info-list-for-prj-form>-->
            </el-tab-pane>
          </el-tabs>
          <el-tabs class="detail-tab" type="card" v-model="activeName">
            <el-tab-pane label="项目费用" name="first">
              <!-- <goods-sale-list-for-prj-fee-form :projectId="form.id" :type="type3"
                                           ref="GoodsSaleListForPrjFeeForm"></goods-sale-list-for-prj-fee-form>-->
              <project-fee-info-list-for-prj-form ref="ProjectFeeInfoListForPrjForm" :projectId="form.id"
                :projectName="form.name"></project-fee-info-list-for-prj-form>
            </el-tab-pane>
          </el-tabs>
          <el-tabs class="detail-tab" type="card" v-model="activeName">
            <el-tab-pane label="开票信息" name="first">
              <project-invoice-detail-list :projectId="form.id"
                ref="ProjectInvoiceInfoListForPrjForm"></project-invoice-detail-list>
            </el-tab-pane>
          </el-tabs>

          <el-tabs class="detail-tab" type="card" v-model="activeName">
            <el-tab-pane label="实际回款" name="first">
              <project-fund-back-list :projectId="form.id" ref="ProjectFundBackListForPrjForm"></project-fund-back-list>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
        <el-tab-pane label="订单产品" name="订单产品">
          <project-sale-list :projectCode="form.code" ref="ProjectSaleListForm"></project-sale-list>
        </el-tab-pane>
        <el-tab-pane label="用料明细" name="用料明细">
          <goods-sale-material-list :projectCode="form.code" ref="GoodsSaleMaterialListForm"></goods-sale-material-list>
        </el-tab-pane>
        <el-tab-pane label="发货记录" name="发货记录">
          <lv-fahuotongzhi-list :projectCode="form.code" ref="ProjectFahuodanListForm"></lv-fahuotongzhi-list>
        </el-tab-pane>
        <el-tab-pane label="报工记录" name="报工记录">
          <goods-saledo-gongxu-user-money-list :projectCode="form.code" ref="GoodsSaledoGongxuUserMoneyListForm"></goods-saledo-gongxu-user-money-list>
        </el-tab-pane>
        <!-- <el-tab-pane label="生产计划" name="生产计划">生产计划</el-tab-pane>
        <el-tab-pane label="生产订单" name="生产订单">生产订单</el-tab-pane> -->
        <el-tab-pane label="生产工单" name="生产工单">
          <paichan-task-yifenpei-list :projectCode="form.code" ref="PaichanTaskYifenpeiListForm"></paichan-task-yifenpei-list>
        </el-tab-pane>
        <el-tab-pane label="工艺工序" name="工艺工序">
          <fenpei-gx-finish-list :projectCode="form.code" ref="ProjectGongxuListForm"></fenpei-gx-finish-list>
        </el-tab-pane>
        <el-tab-pane label="排产任务" name="排产任务">
          <task-not-all-list :projectCode="form.code" ref="TaskNotAllListForm"></task-not-all-list>
        </el-tab-pane>
      </el-tabs>
    </el-form>

    <div class="dialog-footer" slot="footer">
      <el-button @click="submitForm" type="primary">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addAction, getAction, getById, updateAction} from '@/api/manage'

import ProjectSaleList from "../../projectBaseInfo/ProjectSaleList";
import LvFahuotongzhiList from "../../lvYifahuomingxi/LvYifahuomingxiList";
import GoodsSaleMaterialList from "../../goodsSaleMaterial/GoodsSaleMaterialList";
import GoodsSaledoGongxuUserMoneyList from "../../goodsSaledoGongxuUserMoney/GoodsSaledoGongxuUserMoneyList";
import FenpeiGxFinishList from "../../projectBaseInfo/FenpeiGxFinishList";
import TaskNotAllList from "../../projectBaseInfo/TaskNotAllList";
import PaichanTaskYifenpeiList from "../../projectBaseInfo/PaichanTaskYifenpeiList";


import ProjectFeeInfoListForPrjForm from "../../projectFeeInfo/ProjectFeeInfoListForPrjForm";
import GoodsSaleListForPrjForm from "../formDialog/GoodsSaleListForPrjForm";
import GoodsSaleListForPrjFeeForm from "../formDialog/GoodsSaleListForPrjFeeForm";
import ProjectInvoiceInfoListForPrjForm from "../formDialog/ProjectInvoiceInfoListForPrjForm";
import ProjectFundBackListForPrjForm from "../formDialog/ProjectFundBackListForPrjForm";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  treeselect
} from "@/api/system/dept";
import ProjectInvoiceList from "@/views/develop/projectBaseInfo/formDialog/ProjectInvoiceDetailList.vue";
import ProjectInvoiceDetailList from "@/views/develop/projectBaseInfo/formDialog/ProjectInvoiceDetailList.vue";
import ProjectFundBackList from "@/views/develop/projectBaseInfo/formDialog/ProjectFundBackList.vue";

export default {
  name: "ProjectBaseInfoFormForPrj",
  dicts: ['goods_customer', 'company', 'yes_no', 'yewu_type','invoice_back_status'],
  components: {
    ProjectFundBackList,
    ProjectInvoiceDetailList,
    ProjectInvoiceList,
    ProjectFeeInfoListForPrjForm,
    ProjectFundBackListForPrjForm,
    ProjectInvoiceInfoListForPrjForm,
    GoodsSaleListForPrjForm,
    GoodsSaleListForPrjFeeForm,
    Treeselect,
    ProjectSaleList,
    LvFahuotongzhiList,
    GoodsSaleMaterialList,
    GoodsSaledoGongxuUserMoneyList,
    FenpeiGxFinishList,
    TaskNotAllList,
    PaichanTaskYifenpeiList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      salesmanList: [],
      // 表单校验
      rules: {
        projectId: [{required: true,message: "请选择所属项目",trigger: "blur"}],
        code: [{required: true,message: "订单编号不能为空",trigger: "blur"},],
        year: [{required: true,message: "所属年度不能为空",trigger: "blur"}],
        name: [{required: true,message: "订单名称不能为空",trigger: "blur"}],
        salesmanId: [{required: true,message: "请选择业务员",trigger: "blur"}],
        customId: [{required: true,message: "买方不能为空",trigger: "blur"}],
        companyId: [{required: true,message: "项目单位不能为空",trigger: "blur"}],
        goodsType: [{required: true,message: "请选择订单类别",trigger: "blur"}],
        createName: [{required: true,message: "制单人不能为空",trigger: "blur"}],
        area: [{required: true,message: "地区不能为空",trigger: "blur"}],
        contractDate: [{required: true,message: "合同签订日期不能为空",trigger: "blur"}],
      },
      url: {
        getById: '/projectBaseInfo/projectBaseInfo/',
        add: '/projectBaseInfo/projectBaseInfo/add',
        edit: '/projectBaseInfo/projectBaseInfo/edit',
        salesmanList: '/system/role/authUser/allocatedList',
      },
      type2: "2",
      type3: "3",
      // 部门树选项
      deptOptions: [],
    };
  },
  created() {
    this.getTreeselect();
    this.getSalesmanList();
  },
  methods: {
    selectDept(e) {
      let that = this;
      that.$set(that.form, "deptName", e.label);
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    handleUpdateDetail(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "完善项1目信息";
      });
    },
    updateAuditMoney(value) {
      this.$set(this.form, "auditMoney", value);
    },
    selectCustomerForPrj(value) {
      let that = this;
      that.$nextTick(() => {
        let arr = that.$refs.myselectCustomerForPrj;
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].currentValue == value) {
            that.form.customName = arr[i].label;
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        name: null,
        master: null,
        customId: null,
        customName: null,
        companyName: null,
        contractDate: null,
        contractMoney: null,
        auditMoney: null,
        checkDate: null,
        checkState: null,
        fundCallback: null,
        information: null,
        remainMoney: null,
        feeMoney: null,
        taxRemain: null
      };
      this.resetForm("form");
    },
    handleClick(tab, event) {
        console.log(tab, event);
      },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目基础信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单基础信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },

    getSalesmanList() {
      getAction(this.url.salesmanList, {
        "roleId": "e1c2bca31b7944e98f79008c726cf7e8"
      }).then(response => {
        this.salesmanList = response.rows;
      });
    }

  }
};
</script>
